<template>
  <div>
    <div class="lum-dialog-mask" v-show="isShow">
      <el-container class="lum-dialog-box" v-outside="close">
        <el-header class="header" height="50px">
          <p>添加好友</p>
          <p class="tools">
            <el-icon @click="close"><ele-Close /></el-icon>
          </p>
        </el-header>
        <el-main class="main">
          <el-input
            v-model="mobile"
            id="serach-mobile"
            class="input"
            :prefix-icon="Search"
            placeholder="请输入对方手机号(精确查找)"
            clearable
            @keyup.enter.prevent="onSubmit"
            @input="error = false"
          />
          <p v-show="error" class="error">
            无法找到该用户，请检查搜索内容并重试
          </p>
          <el-button
            type="primary"
            size="default"
            :loading="loading"
            @click="onSubmit"
            >立即查找
          </el-button>
        </el-main>
      </el-container>
    </div>
    <user-card-detail v-model:show="showUserDetail" :user_id="user_id"></user-card-detail>
  </div>
</template>

<script>
import { Search } from '@element-plus/icons-vue'
import { reactive, toRefs, nextTick } from 'vue'
import { useChatApi } from '/@/api/chat/index'

export default {
    name: 'UserSearch',
    setup() {
        const chatApi = useChatApi()
        const state = reactive({
            loading: false,
            isShow: false,
            mobile: '',
            error: false,
            showUserDetail: false,
            user_id: ''
        })
        // 显示窗口
        const open = () => {
            state.mobile = ''
            state.isShow = true
            nextTick(() => {
                document.getElementById('serach-mobile').focus()
            })
        }
        // 关闭窗口
        const close = () => {
            state.isShow = false
        }
        const onSubmit = () => {
            let { mobile } = state
            if (mobile == '') return false
            state.loading = true
            chatApi.ServeSearchContact({
                mobile,
            }).then(res => {
                if (res.code == 0) {
                    state.user_id = res.data.id
                    state.showUserDetail = true
                    close()
                }
            }).finally(() => {
                state.loading = false
            }).catch(() => {
                state.error = true
            })
        }
        return {
            ...toRefs(state),
            Search,
            onSubmit,
            close,
            open
        }
    }
}
</script>

<style lang="scss" scoped>
.lum-dialog-box {
  width: 450px;
  max-width: 450px;
  height: 250px;
  .main {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    .input {
      width: 85%;
    }
    .error {
      width: 85%;
      color: var(--el-color-danger);
      font-size: 12px;
      height: 50px;
      line-height: 50px;
    }

    button {
      margin-top: 20px;
      width: 100px;
    }
  }
}
</style>